using System;
using System.Collections.Generic;

namespace BearGame.EventSystem
{
    public class EventSubscriber : IDisposable
    {
        private readonly List<Tuple<Type, object>> _subscriptions = new List<Tuple<Type, object>>();

        public void Subscribe<T>(Action<T> handler) where T : class, IEvent, new()
        {
            _subscriptions.Add(new Tuple<Type, object>(typeof(T), handler));
            EventDispatcher.Register(typeof(T), handler);
        }
        
        public void Dispose()
        {
            foreach (var (type, action) in _subscriptions)
            {
                EventDispatcher.Unregister(type, action);
            }
            _subscriptions.Clear();
        }
    }
}